package com.crm.converter;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		HttpSession session = request.getSession();
		if(handler.getClass() == HandlerMethod.class){
			HandlerMethod handlerMethod = (HandlerMethod) handler;
			Method method = handlerMethod.getMethod();
			if(method.getName().equals("tologin")){
				
				return true;
			}else{
				if(session.getAttribute("userinfo") != null){
				
					return true;
				}else{
					response.sendRedirect("tologin");
					
					return false;
				}
			}
		}
		return true;
	}

}
